package com.zadtek.shopmall.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.bean.HomeShopBean;
import com.zadtek.shopmall.bean.IndexProductItem;
import com.zadtek.shopmall.bean.MultiItem;

import java.util.List;

/**
 * Created by Zackv on 2018/4/3.
 */

public class HomeAdapter extends BaseMultiItemQuickAdapter<MultiItem, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    private Context context;

    public HomeAdapter(Context context,List<MultiItem> data) {
        super(data);
        this.context = context;
        addItemType(MultiItem.Type_Product, R.layout.product_item);
        addItemType(MultiItem.Type_Home_Header, R.layout.head_item);
        addItemType(MultiItem.Type_Home_Shop, R.layout.home_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItem item) {
        switch (helper.getItemViewType()){
            case MultiItem.Type_Product:
                helper.addOnClickListener(R.id.iv_add_to_car);
                IndexProductItem data2 = (IndexProductItem) item.getData();
                Glide.with(context).load(data2.getProductImgUrl()).into((ImageView) helper.getView(R.id.product_img));
                helper.setText(R.id.product_price,data2.getProductPrice());
                helper.setText(R.id.product_title,data2.getProductTitle());
                helper.setText(R.id.product_sales,data2.getProductStock()+"  "+data2.getProductSales());
                break;
            case MultiItem.Type_Home_Shop:
                HomeShopBean homeShopBean = (HomeShopBean) item.getData();
                helper.setText(R.id.tv_time,homeShopBean.getDistance()+"m|"+homeShopBean.getTime()+"分钟");
                helper.setText(R.id.tv_delivery,"￥"+homeShopBean.getShop_song()+"起送|配送费￥"+homeShopBean.getShipping_fee());
                helper.setText(R.id.tv_sales,homeShopBean.getRank() + "    月销售"+homeShopBean.getNum()+"单");
                SimpleRatingBar simpleRatingBar = helper.getView(R.id.rb_shop_star);
                simpleRatingBar.setRating(homeShopBean.getRank());
                helper.setText(R.id.tv_shop_name,homeShopBean.getShop_name());
                Glide.with(context).load(homeShopBean.getShop_logo()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.shop_img));
                break;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type){
                        case MultiItem.Type_Product:
                            return 1;
                        default:
                            return 2;
                    }
                }
            });
        }
    }
}
